Page({

  /**
   * 页面的初始数据
   */
  data: {
      searchValue: "",
      searchHistory: [] as Array<string>,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // 写入缓存
    try {
        let searchHistoryString = wx.getStorageSync('searchHistory')
        if (searchHistoryString) {
            let searchHistory = JSON.parse(searchHistoryString)
            this.data.searchHistory = searchHistory
            this.setData({
                searchHistory: searchHistory
            })
        }
      } catch (e) { 

      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(_pts): WechatMiniprogram.Page.ICustomShareContent {
      return {}
  },
  // 搜索框输入事件
  searchChange(input: WechatMiniprogram.Input) {
      this.data.searchValue = input.detail.value
      this.setData({
          searchValue: this.data.searchValue
      })
  },
  // 点击搜索去搜索结果页
  searchTap(_tap: WechatMiniprogram.ControlTap) {
      let searchValue = this.data.searchValue
      // 写入本地变量
      this.data.searchHistory.push(searchValue)
      // 写入缓存
      try {
        wx.setStorageSync('searchHistory', JSON.stringify(this.data.searchHistory))
      } catch (e) { 

      }
      this.data.searchValue = ""
      this.setData({
          searchValue: this.data.searchValue,
          searchHistory: this.data.searchHistory
      })
      wx.navigateTo({
          url: "/pages/search/searchResult/index?search_value=" + searchValue
      })
  }
})

// 解决无法重新声明块级范围变量的问题
export {}